package com.example.gymmanagement.controller;


import com.example.gymmanagement.Utils.Result;
import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.domain.Bill;
import com.example.gymmanagement.service.BillService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/bill")
public class BillController {
    @Autowired
    private BillService billService;

    //新增账单
    @PostMapping("/add")
    @ApiOperation("新增账单")
    public Result add(@RequestBody Bill bill,@RequestHeader("Authorization") String token){
        //账单生成时间
        bill.setBillTime(LocalDateTime.now());
        boolean flag = this.billService.insert(bill);
        String msg = flag?"提交成功":"提交失败";
        return new Result(flag? StatusCode.SAVE_OK:StatusCode.SAVE_ERR,flag,msg);
    }
    @PostMapping("/delete/{id}")
    @ApiOperation("按id删除账单")
    public Result delete(@PathVariable Integer id,@RequestHeader("Authorization") String token){
        boolean flag = this.billService.delete(id);
        String msg = flag?"删除成功":"删除失败";
        return new Result(flag?StatusCode.SAVE_OK:StatusCode.SAVE_ERR,flag,msg);
    }
    @PostMapping("/update")
    @ApiOperation("修改账单")
    public Result update(@RequestBody  Bill bill,@RequestHeader("Authorization") String token){
        bill.setBillUpdatetime(LocalDateTime.now());
        boolean flag = this.billService.updateBill(bill);
        String msg = flag?"修改成功":"修改失败";
        return new Result(flag?StatusCode.UPDATE_OK:StatusCode.UPDATE_ERR,flag,msg);
    }

    @GetMapping("/select")
    @ApiOperation("查询某个用户(当前登录用户)的账单（类别模糊查询，已分页）")
    public Result selectByCategory( Integer billUser, String billtype,Integer index,Integer size,@RequestHeader("Authorization") String token){
        Result billList = this.billService.selectByCategory(billUser,billtype,index,size);
        return billList;

    }

    @GetMapping
    @ApiOperation("查询全部账单(分页)")
    public Result selectALlBill(Integer index,Integer size,@RequestHeader("Authorization") String token){
        Result result = this.billService.selectAllBill(index,size);
        return result;
    }
    @GetMapping("/cal")
    @ApiOperation("总收入/支出")
    public Result calculateBills(@RequestHeader("Authorization") String token){
        Result result = this.billService.calculateBills();
        return result;
    }
}














